<!--#include file="../nav/admin_top.php" -->  
<script type="text/javascript">
var url_save = '<?=url::build('admin_ku','add',array('reqtype'=>'json'))?>'; 
var url_return = '<?=url::build('admin_ku','index')?>';   
var nowindex = 0;
var veges = <?=json_encode($veges)?>;
$().ready(function(){  
	showLoading();   
  $('#name').focus();
	o2js.build('dates'); 
	o2js.build('jscalendar');
	//fnAddPro();
	Calendar.setup({
		inputField:'plandate',
		button:'slt_plandate',
		ifFormat:"%Y-%m-%d",
		showsTime:false
	});
	$('#plandate').val(addDay(new Date(),3).eformat('yyyy-MM-dd'));
});


/* 设置产品项序号 */
function fnSetIndex(){
	var i = 1;
	$('td.show_index').each(function(e){
		$(this).html(i++);
	});
}

/* 添加产品 */
function fnAddPro(){
	GfnSelectVege('fnAddProBuild','选择菜品');
}
function fnAddProBuild(e){
	for(var i=0; i<e.vege_ids.length ; i++){
		var obj = e.vege_ids[i];
		var tmp_goon = true;
		//判断是否已经添加
		$('.vege_id').each(function(){
			if(obj.id ==  $(this).attr('val')){
				tmp_goon = false;
			}
		});
		if(tmp_goon){
			obj.price = veges[obj.id].price;
			var html = '';
			html += '<tr class="vegetr" id="vegetr_'+nowindex+'" i="'+nowindex+'">';
			html += ' <td id="show_index_'+nowindex+'" class="show_index"></td>';
			html += ' <td id="vege_id_'+nowindex+'" class="vege_id" val="'+obj.id+'" price="'+obj.price+'" >';
			html += obj.name;
			html += ' </td>';
			html += ' <td >';
			html += ' <input id="vege_num_'+nowindex+'" value="100" type="text" class="textBox"  style="width:80px;text-align:right" /> KG';
			html += ' </td>';
			html += ' <td>';
			html += ' 	<img title="点击移除本行" src="'+baseUrl +'misc/images/icos/del.gif" class="sltimg" onclick="fnDelVege('+nowindex+')" />';
			html += ' </td>';
			html += '</tr>';
			$('#veges').append(html);
			nowindex++;
		}
	}	
	fnSetIndex();
}
function fnDelVege(id){
	$('#vegetr_'+id).remove();
	fnSetIndex();
}
/* 保存 */
function fnSave(){ 
	var form = serializeObj("#myform");
	var veges = [];
	if(form.name == ''){
		alert('请填写【库存批次标识】');
		$('#name').focus();
		return false;
	}
  
	//包含蔬菜
	$('#veges tr.vegetr').each(function(){
		var i = $(this).attr('i');
		veges.push({
			vege_id:$('#vege_id_'+i).attr('val'),
			plan_weight:$('#vege_num_'+i).val()
		});
	});
	if(veges.length <= 0){
		alert('请添加菜品');
		return false;
	}
	for(var i=0; i<veges.length; i++){
		if(veges[i].plan_weight == '' || !fnRegExp('reg_num1',veges[i].plan_weight)){
			alert('请正确填写第'+(i+1)+'行的供应计划！');
			return false;
		}
	}
  $('button').attr('disabled','disabled');
	$.ajax({
		async:true,
		type:'post',
		url:url_save,
		data:{
			form:JSON.encode(form),
			veges:JSON.encode(veges)
		},
		success:function(e){
  		$('button').attr('disabled','');
			var ok = errorHandler(e);
			if(ok){　 
				jumpTo(url_return);
			}
		}
	});
}
</script>
<div class="titleDiv">
	<div class="titleCaption">
		<span class="captionSpan">库存批次 - 添加</span>
	</div>
</div>
<div  class="button_div">
		<button class="btn_save" onclick="fnSave()">保 存</button>
		<span class="gray">或</span> 
		<button class=" " onclick="jumpTo(url_return);return false;">返 回</button>
</div>  
<form id="myform" action="" method="post" onsubmit="return false;">
<div class="blockdiv">
	<div class="titlediv">基本信息</div>
	<table class="mytable" > 
		<tr>
			<td class="mylabel"><span class="red">*</span>库存批次标识：</td>
			<td class="myinfo" > 
				<input type="text" class="textBox" id="name" name="name" maxlength="100"  />
			</td>  
		</tr> 
		<tr>
			<td class="mylabel"><span class="red">*</span>计划到货日期：</td>
			<td class="myinfo" > 
				<input type="text" class="textBox" id="plandate" name="plandate" readonly="readonly" />
				<input type="button" class="btnicon_calendar" id="slt_plandate" />
			</td>  
		</tr>  
	</table> 
</div>
</form>
<div class="blockdiv">
	<div class="titlediv">菜品</div>
	<div style="margin:10px auto">
		<a href="javascript:void(0)" onclick="fnAddPro()"><img src="/misc/images/icos/add2.gif" />添加菜品</a>
	</div>
	<div style="margin:5px auto">
		<table class="innserTb">
			<thead>
				<tr>
					<th style="width:60px">序号</th>
					<th style="width:250px">菜品</th>
					<th>供应计划（KG）</th>
					<th style="width:40px">移除</th>
				</tr>
			</thead>
			<tbody id="veges">
		 
			</tbody>
		</table>
	</div>
</div>	
<div  class="button_div">
	<button class="btn_save" onclick="fnSave()">保 存</button>
	<span class="gray">或</span> 
	<button class=" " onclick="jumpTo(url_return);return false;">返 回</button>
</div>  
<!--#include file="../nav/admin_bottom.php" -->